package client;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.UnknownHostException;

import client.messageContent.BalanceEnquiryMsgContent;
import client.messageContent.CloseAccountMsgContent;
import client.messageContent.CreateAccountMsgContent;
import client.messageContent.DepositMsgContent;
import client.messageContent.MonitorMsgContent;
import client.messageContent.TransferFundMsgContent;
import client.messageContent.WithdrawMsgContent;
import common.Constants;
import common.data.Message;
import common.data.MessageTypeEnum;
import common.data.RequestTypeEnum;
import common.interfaces.MessageContent;

public class RequestCreator {
	private static RequestCreator current;
	private long currentRequestId = 0;
	private InetAddress host;
	private MessageContent msgContent;
	
	
	public RequestCreator(String serverIP){
		try {
			this.host = InetAddress.getByName(serverIP);
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static RequestCreator getCurrent(String serverIP){
		if (current == null) {
			current = new RequestCreator(serverIP);
		}
		return current;
	}

	public static RequestCreator getCurrent(){
		return current;
	}
	public DatagramPacket CreateMessage(RequestTypeEnum msgType, Object msgContent){
		DatagramPacket request = null;
		Message msg;
		this.msgContent = (MessageContent) msgContent;
		switch (msgType) {
			case CREATE_ACCOUNT:
				CreateAccountMsgContent createAccountMsgContent = (CreateAccountMsgContent) this.msgContent;
				msg = new Message(MessageTypeEnum.REQUEST, currentRequestId++, msgType, createAccountMsgContent);
				System.out.println("current request id at client " + currentRequestId);
				request = new DatagramPacket(msg.serialize(), msg.getLength(), this.host, Constants.SERVER_PORT);
				break;
			case CLOSE_ACCOUNT:
				CloseAccountMsgContent closeAccountMsgContent = (CloseAccountMsgContent) this.msgContent;
				msg = new Message(MessageTypeEnum.REQUEST,currentRequestId++,msgType,closeAccountMsgContent);
				request = new DatagramPacket(msg.serialize(),msg.getLength(),this.host,Constants.SERVER_PORT);
				break;
			case DEPOSIT_ACCOUNT:
				DepositMsgContent depositMsgContent = (DepositMsgContent) this.msgContent;
				msg = new Message(MessageTypeEnum.REQUEST,currentRequestId++,msgType,depositMsgContent);
				request = new DatagramPacket(msg.serialize(),msg.getLength(),this.host,Constants.SERVER_PORT);
				break;
			case WITHDRAW_ACCOUNT:
				WithdrawMsgContent withdrawMsgContent = (WithdrawMsgContent) this.msgContent;
				msg = new Message(MessageTypeEnum.REQUEST,currentRequestId++,msgType,withdrawMsgContent);
				request = new DatagramPacket(msg.serialize(),msg.getLength(),this.host,Constants.SERVER_PORT);
				break;
			case BALANCE_ENQUIRY:
				BalanceEnquiryMsgContent balEnquiryMsgContent = (BalanceEnquiryMsgContent) msgContent;
				msg = new Message(MessageTypeEnum.REQUEST,currentRequestId++,msgType,balEnquiryMsgContent);
				request = new DatagramPacket(msg.serialize(),msg.getLength(),this.host,Constants.SERVER_PORT);
				break;
			case TRANSFER_SERVICE:
				TransferFundMsgContent transferMsgContent = (TransferFundMsgContent) msgContent;
				msg = new Message(MessageTypeEnum.REQUEST,currentRequestId++,msgType,transferMsgContent);
				request = new DatagramPacket(msg.serialize(),msg.getLength(),this.host,Constants.SERVER_PORT);
				break;
			case MONITOR_SERVER:
				MonitorMsgContent monitorMsgContent = (MonitorMsgContent) this.msgContent;
				msg = new Message(MessageTypeEnum.REQUEST, currentRequestId++, msgType, monitorMsgContent);
				System.out.println("current request id at client " + currentRequestId);
				request = new DatagramPacket(msg.serialize(), msg.getLength(), this.host, Constants.SERVER_PORT );
		}
		return request;
	}
	
	public MessageContent getMsgContent(){
		return msgContent;
	}
	public void setMsgContent(MessageContent msgContent){
		this.msgContent = msgContent;
	}
}
